package com.lifei.mvc.controller;


import com.lifei.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping("testString")
    public String testString(){
        return "error";
    }
    @RequestMapping("testSession")
    public ModelAndView testSession(HttpSession session){
        session.setAttribute("adminCode", "110");
        return new ModelAndView("success");
    }
    private String errorMsg="系统异常，请稍后重试或联系MsHao";
    @ModelAttribute("errorMsg")
    public String ToolTips(){
        return errorMsg;
    }
    @RequestMapping("getToolTips")
    public String getToolTips(){
        return "success";
    }

    @RequestMapping("testMAB")
    public ModelAndView testMAB(@ModelAttribute(value = "age") int age){
        return new ModelAndView("success");
    }
    @RequestMapping("testModel")
    public ModelAndView testModel(Model model){
        //绑定数据到Model对象中
        model.addAttribute("m1","门票和车票");
        model.addAttribute("m2","需要RMB");
        //返回封装结果到页面
        return new ModelAndView("success");
    }

    @RequestMapping("testMAV")
    public ModelAndView testMAV(){
        Map<String,Object> map=new HashMap<>();
        map.put("map1", "诗和远方");
        map.put("map2", "在向我们招手");
        map.put("map3", "123");
        ModelAndView mav = new ModelAndView("success",map);
        return mav;
    }

    @RequestMapping("testReq")
    public String testReq(HttpServletRequest request){
        //绑定数据到request对象上
        request.setAttribute("str", "我说OK你说NO!");
        return "success";

    }
    @RequestMapping("testRedirect")
    public String testRedirect(){
        System.out.println("测试请求的重定向");
        //重定向
        return "redirect:/success1.jsp";
    }
    @RequestMapping("testUser")
    public String testUser(User user){
        System.out.println("username="+user.getUsername());
        System.out.println("age="+user.getAge());
        System.out.println("addr="+user.getAddr());
        return "/success.jsp";
    }
    @RequestMapping("testRequestParam")
    public String testRequestParam(@RequestParam("name") String username,
                                  Integer age){
        System.out.println("username="+username+";"+"age="+age);
        return "/success.jsp";
    }
    @RequestMapping("testRequest")
    public String testRequest(HttpServletRequest request){
        //接收前端传递的name参数的值
        String name = request.getParameter("name");
        //接收前端传递的age参数的值
        //String age1 = request.getParameter("age");
        Integer age = Integer.parseInt(request.getParameter("age"));
        System.out.println(name+";"+age);
        return "/success.jsp";
    }
    @RequestMapping("quickstart")
    public String Hello(){
        return "/success.jsp";
    }
//    @RequestMapping("/Hello2")
//    public String Hello2(){
//        return "success";
//    }
    @RequestMapping("welcome")
    @ResponseBody
    public ModelAndView index(){
        //创建模型和视图
        ModelAndView mav=new ModelAndView();
        //将咱们想展示的内容封装到ModelAndView中
        mav.addObject("msg1","社会很单纯");
        mav.addObject("msg2","复杂的是人啊");
        mav.setViewName("welcome");
        return mav;
    }

}
